﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SuperMarketSystem
{
    public partial class ConfirmTrueItem : Form
    {
		TrueItem trueItem=null;
        public ConfirmTrueItem()
        {
            InitializeComponent();//初始化组件
        }

        private void btnConfirm_Click(object sender, EventArgs e)//确认按钮点击事件
        {
			if (!textItemSum.Text.Equals(""))//判断是否为空
			{
				trueItem = new TrueItem();
				trueItem.ID=Convert.ToInt32(textItemID.Text);
				trueItem.num=Convert.ToInt32(textItemNum.Text);
				trueItem.priceafterdiscount=Convert.ToDouble(textItemPrice.Text);
				trueItem.realSumPrice=Convert.ToDouble(textItemSum.Text);
				trueItem.store=Convert.ToInt32(textItemStore.Text);
				trueItem.min=Convert.ToInt32(textItemMin.Text);
				trueItem.name = textItemName.Text;
				this.Close();
			}
			else
			{
				MessageBox.Show("无效操作", "商品ID或者商品欲购买数量不合乎要求");
			}
		}
		public TrueItem returnTrueItem()//返回一个真实商品类
        {
			return trueItem;
        }
        private void textItemNum_TextChanged(object sender, EventArgs e)//检测文本框数量内容变更
        {
			if (!(textItemPrice.Text.Equals("")) && !(textDiscount.Text.Equals("")))
			{
				if (MyOtherUtils.checkInt(textItemNum.Text.Trim()))
				{
					int wantToBuy = Convert.ToInt32(textItemNum.Text.Trim());
					int itemStore = Convert.ToInt32(textItemStore.Text);
					int itemMin = Convert.ToInt32(textItemMin.Text);
					double itemDiscount = Convert.ToDouble(textDiscount.Text);
					double itemPrice = Convert.ToDouble(textItemPrice.Text);
					if (itemStore - wantToBuy < itemMin)
					{
						textItemNum.Text=(Convert.ToString(itemStore - itemMin));
						textItemSum.Text=(Convert.ToString((itemStore - itemMin) * itemDiscount * itemPrice));
					}
					else
					{
						textItemSum.Text=(Convert.ToString((wantToBuy) * itemDiscount * itemPrice));
					}
				}
			}
			else
			{
				textItemSum.Text="";
			}
		}

        private void textItemID_TextChanged(object sender, EventArgs e)//检测文本框ID内容变更
        {
			String itemNameString = textItemID.Text.Trim();
			SqlConnection connnetion = MySqlServerUtils.getMySqlServerConnection();
			try
			{
				String loginsqlString = String.Format("select * from stores where itemID={0}",Convert.ToInt32(itemNameString));//检查是否存在该商品ID
				SqlCommand mySqlCommand = new SqlCommand(loginsqlString, connnetion);
				SqlDataReader set = mySqlCommand.ExecuteReader(); 
				if (set.Read())//存在则显示全部参数
				{
					String itemName = set["itemname"].ToString();
					String itemType = set["itemtype"].ToString();
					String itemFrom = set["itemfrom"].ToString();
					String itemCal = set["itemcal"].ToString();
					Double itemPrice = Convert.ToDouble(set["itemprice"]);
					Double itemDiscount = Convert.ToDouble(set["itemdiscount"]);
					int itemStore = Convert.ToInt32(set["itemstore"]);
					int itemMin = Convert.ToInt32(set["itemmin"]);
					int providerID = Convert.ToInt32(set["providerID"]);
					textItemName.Text = itemName;
					textItemType.Text=itemType;
					textItemFrom.Text=itemFrom;
					textItemCal.Text=itemCal;
					textItemPrice.Text=itemPrice.ToString();
					textDiscount.Text=itemDiscount.ToString();
					textItemStore.Text=itemStore.ToString();
					textItemMin.Text=itemMin.ToString();
					textProviderID.Text=providerID.ToString();
					if (MyOtherUtils.checkInt(textItemNum.Text.Trim()))
					{
						int wantToBuy = Convert.ToInt32(textItemNum.Text.Trim());
						if (itemStore - wantToBuy < itemMin)
						{
							textItemNum.Text = (Convert.ToString(itemStore - itemMin));
							textItemSum.Text = (Convert.ToString((itemStore - itemMin) * itemDiscount * itemPrice));
						}
						else
						{
							textItemSum.Text = (Convert.ToString((wantToBuy) * itemDiscount * itemPrice));
						}
					}
				}
				else//否则把所有显示的参数都变为空
				{
					textItemName.Text="";
					textItemType.Text="";
					textItemFrom.Text="";
					textItemCal.Text="";
					textItemFrom.Text="";
					textItemPrice.Text="";
					textDiscount.Text="";
					textItemStore.Text="";
					textItemMin.Text="";
					textProviderID.Text="";
					textItemSum.Text="";
				}
				MySqlServerUtils.closeSqlServerConnection(connnetion);
			}
			catch (Exception e1)
			{
				// TODO Auto-generated catch block
				
			}
		}
    }
}
